zsApp
.controller( 'medalController',["$scope", "$http", "$state" , function( $scope, $http, $state ){
	// 储存ID
	$scope.ipRuleId = [];
	// 详情
	$scope.detailsInfo = {};
	// 修改
	$scope.editInfo = {};

	// 分页
	function dataRendering() {
		$scope.paginationConf = {
	        currentPage: 1,
	        totalItems: 100,
	        itemsPerPage: 10,
	        pagesLength: 15,
	        perPageOptions: [10, 20, 30, 40, 50],
	        onChange: function(){
	        	var self = this;
	        	if( (($scope.isRequestCurrentPage != self.currentPage) || ($scope.isRequestItemsPerPage != self.itemsPerPage)) && (new Date() - $scope.nowTime > 500) )
	        	{	
	        		$scope.nowTime = new Date();
					$http
					({
						url  : $scope.zsUrl+'digitalhome-boss/Admin/iprules/getList',
						method : 'POST',
						data   : {
							pageNumber  : self.currentPage,  
							pageSize 	: self.itemsPerPage,  
						}
					})
					.then
					(
						function success(res) {
							// IP规则列表页面表格
							$scope.tabVal = res.data.result;
							// 遍历所有IP规则ID
							angular.element(res.data.result).each(function(i) {
								$scope.ipRuleId[i] = this.ipRuleId;
							});
							// 总共有多少条数据
							self.totalItems = res.data.length;

						}, 
						function error(err) {
							console.log(err);
						}
					);

					$scope.isRequestCurrentPage = self.currentPage;
					$scope.isRequestItemsPerPage = self.itemsPerPage;

				}
	        }
	    }
    }
    dataRendering();
	
	
	function isValidIP(ip) {
	    var reg = /^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/
	    return reg.test(ip);
	} 

	// 确定新增（新增一条IP规则列表数据）
	$scope.confirmNewlyAdded = function() {	
		// 新增IP规则信息（封装成JSON）
		var iprulesData = {
			ipRuleId   : $scope.details_ipRuleId,
			ruleName   : $scope.details_ipRulesName,
			sortIndex  : $scope.details_sortIndex,
			ruleMode   : $scope.details_ruleMode,
			expression : $scope.details_expression,
			enabled    : $scope.details_enabled,
			
		}
		console.log($scope.details_expression);

		$http
		({
			url    : $scope.zsUrl+'digitalhome-boss/Admin/iprules/addIprules',
			method : 'POST',
			data   : iprulesData, /* 将JSON发送给服务器 */
 
 			
		}).then(function success(res) {
			console.log(res.status);
		},
		function error(err){
			console.log(err);
		});
		// 关闭窗口
		$('#newlyAdded').modal('hide');
	}


	// 详情（用于展示从服务器获取的IP规则）
	$scope.details = function($event, $index) {
		console.log($scope.ipRuleId[$index]);
		$http
		({
			url    : $scope.zsUrl+'digitalhome-boss/Admin/iprules/getIprulesInfo/'+$scope.ipRuleId[$index]+'',
			method : 'GET',
		})
		.then(
			function success(res) 
			{
				$scope.details_ipRuleId = res.data.result.ipRuleId;
				$scope.details_ruleName = res.data.result.ruleName;
				$scope.details_sortIndex = res.data.result.sortIndex;
				$scope.details_ruleMode = res.data.result.ruleMode;
				$scope.details_expression = res.data.result.expression;
				$scope.details_enabled = res.data.result.enabled;
				$scope.details_userName = res.data.result.userName;
					//console.log(res.data);
				
			},
			function error(err) 
			{
				console.log(err);
			}
		);
	}

	// 修改（调用接口与“详情”相同，用于展示从服务器获取的IP规则）
	var __index = 0;
	$scope.edit = function($event, $index) {
		__index = $index;
		$http({
			url    : $scope.zsUrl+'digitalhome-boss/Admin/iprules/getIprulesInfo/'+$scope.ipRuleId[$index]+'',
			method : 'POST',
		})
		.then(
			function success(res) {
				console.log(res.data.result.sortIndex);
				// 展示从服务器获取到的IP规则数据
				$scope.details_ipRulesName = res.data.result.ruleName;
				$scope.details_sortIndex = res.data.result.sortIndex;
				$scope.details_ruleMode = res.data.result.ruleMode;
				$scope.details_expression = res.data.result.expression;
				$scope.details_enabled = res.data.result.enabled;
			},
			function error() {

			}
		);

	}

	// 确认修改（用于修改原有的IP规则）
	$scope.confirmEdit = function($event) {

		// 修改角色组名、描述（封装成JSON）
		var editData = {
			ipRuleId    : $scope.ipRuleId[__index],
			ruleName    : angular.element($event.target).parents('.modal-content').find('input[name=0]').val(),
			sortIndex   : angular.element($event.target).parents('.modal-content').find('input[name=1]').val(),
			ruleMode    : angular.element($event.target).parents('.modal-content').find('input[name=2]').val(),
			expression  : angular.element($event.target).parents('.modal-content').find('input[name=3]').val(),
			enabled     : angular.element($event.target).parents('.modal-content').find('input[name=4]').val(),
		}

		$http(
		{
			url   	: $scope.zsUrl+'digitalhome-boss/Admin/iprules/modify',
			method  : 'POST',
			data    : editData, /* 将JSON发送给服务器 */
		})
		.then(function success(res) 
		{
			console.log(res);
		}, function error(err) 
		{
			console.log(err);
		});
	}

	// 删除（保存当前点击对象的$index、this指向）
	var _this = '', _index = 0;
	$scope.delete = function($event, $index) {
		
		// 当前对象（this指向不变）
		_this = angular.element($event.target);
		// 每一行的序列号
		_index = $index;
	}

	// 确定删除（用于删除IP规则数据）
	$scope.confirmDel = function($event) {

		console.log(_index);
		$http
		({
			url: $scope.zsUrl+'digitalhome-boss/Admin/iprules/delete/'+$scope.ipRuleId[_index]+'',
			method: 'GET',
			data: { ipRuleId: $scope.ipRuleId[_index] }, /* 给服务器发送需要删除的数据对应的ID */
		})
		.then
		(
			function success(res) {
				console.log(_index);
				// result_code == '0000' 则删除，否则不删除
				if( '0000' == res.data.result_code )
				{
					_this.parents('tr').remove();
				}
				else 
				{
					console.log(res.data);
				}
			},
			function error(err) {
				console.log(err);
			}
		);

	}


}]);

